<template>
  <div>
    <Navbar appTitle="派发至车间" />
    <div class="search-content wrap-apply">
      <van-cell-group>
        <p class="label required FormTit"><span></span>生产负责人</p>
        <div class="haveUnitBox">
          <van-field
            v-model="formData.chargeUserIdTxt"
            @click="selectShow = true"
            :is-link="!formData.chargeUserIdTxt"
            readonly
            placeholder="请选择"
          >
            <div
              slot="right-icon"
              v-show="!!formData.chargeUserIdTxt"
              @click.stop="onChangeUserName('', '')"
            >
              <img
                src="~@/assets/img/filedClear.png"
                alt=""
                style="width: 0.16rem"
              />
            </div>
          </van-field>
          <ContactPickers
            :visible="selectShow"
            ref="mychild"
            :max="1"
            @closePicker="closePicker"
            @confirm="onChangeUserName"
          />
        </div>
        <div class="bottomLine"></div>
      </van-cell-group>
      <van-cell-group>
        <p class="label required FormTit"><span></span>工艺路线</p>
        <van-field-select-picker
          :supportClear="true"
          label=""
          placeholder="请选择"
          v-model="formData.processRouteId"
          :columns="options"
          :option="{ label: 'craftName', value: 'id' }"
          @confirm="processConfim"
        />
        <div class="bottomLine"></div>
      </van-cell-group>
      <!-- <div class="search-bottom">
        <span class="btnconfirm" @click="distributeOk">提交</span>
      </div> -->
      <div style="height: 1rem; background: #f5f5f5"></div>
      <div class="subimtForm">
        <van-button
          class="button"
          :loading="loading"
          type="info"
          @click="distributeOk"
          >提交</van-button
        >
      </div>
    </div>
  </div>
</template>
<script>
import Navbar from "@/components/navBar/navBar_right";
import VanFieldSelectPicker from "@/components/ActionSheet/VanFieldSelectPicker";
import { getAction, postAction } from "@/api/manage";
import ContactPickers from "@/components/choiceuse/ContactPicker";
export default {
  name: "search",
  components: {
    Navbar,
    VanFieldSelectPicker,
    ContactPickers,
  },
  data() {
    return {
      formData: {},
      options: [],
      selectShow: false,
      loading: false,
    };
  },
  computed: {},
  // 路由返回时先判断当前页是否存在弹窗，有就先关闭弹窗不返回，直到所有弹窗关闭再返回
  beforeRouteLeave(to, from, next) {
    let that = this;
    if (that.selectShow) {
      that.selectShow = false;
      next(false);
    } else {
      next();
    }
  },
  mounted() {},
  created() {
    getAction("/planmgt/erpProductOrder/getProcessRouteByGoodsId", {
      goodsId: this.$route.query.goodsId,
    }).then((res) => {
      console.log(res, "res");
      this.options = res.result;

      if (this.options.length > 0) {
        this.formData.processRouteId = this.options[0].id;
      }
    });
  },
  methods: {
    onChangeUserName(a, b) {
      if (!b) {
        this.formData.chargeUserIdTxt = "";
        this.formData.chargeUserId = "";
        this.selectShow = false;
        this.$forceUpdate();
        return;
      }
      this.formData.chargeUserIdTxt = b[0].realname;
      this.formData.chargeUserId = b[0].id;
      console.log(this.formData);
      this.selectShow = false;
    },
    closePicker() {
      this.selectShow = false;
    },
    processConfim(v) {
      console.log(v, this.formData, "11");
    },
    distributeOk() {
      const { chargeUserId, processRouteId } = this.formData;
      if (!chargeUserId) {
        return this.$toast("请选择生产负责人");
      }
      if (!processRouteId) {
        return this.$toast("请选择工艺路线");
      }
      let par = {
        chargeUserId,
        id: this.$route.query.id,
        processRouteId,
      };
      console.log(this.formData, par, "fff");
      this.loading = true;
      postAction("/planmgt/erpProductOrder/distribute", par).then((res) => {
        if (res.code === 200) {
          this.$toast(res.message);
          this.$router.go(-1);
          this.loading = false;
        } else {
          this.$toast(res.message);
          this.loading = false;
        }
      });
    },
  },
};
</script>
<style lang="scss" scoped>
@import "@/assets/theme.scss";
.search-content {
  //  padding-bottom: 0.5rem;
  position: fixed;
  top: 0.42rem;
  width: 100%;
  height: calc(100vh - 1.12rem);
  background: #f5f5f5;
  overflow: scroll;
  z-index: 99;
  .formgroup {
    background: #ffffff;
    // padding: 0 0.16rem;
    margin-top: 0.1rem;
    margin-bottom: 0.2rem;
    padding-top: 0.1rem;

    .title {
      width: 0.58rem;
      height: 0.14rem;
      font-size: 0.15rem;
      font-family: PingFang SC;
      font-weight: 400;
      color: #191f25;
      line-height: 0.05rem;
      margin-left: 0.16rem;
    }
  }
  .subimtForm {
    height: 0.6rem;
    line-height: 0.5rem;
    width: 100%;
    background: #fff;
    position: fixed;
    bottom: 0px;
    .button {
      width: 94vw;
      height: 0.39rem;
      text-align: center;
      margin-left: 3vw;
    }
  }
}
.bottomLine {
  height: 0.01rem;
  background-color: #f0f0f0;
  margin: 0 0.16rem;
}
.otherText {
  padding-bottom: 0.16rem;
  font-size: 0.16rem;
  font-weight: bold;
  color: #191f25;
}
.haveUnitBox {
  display: flex;
  justify-content: space-between;
  align-items: center;
  box-sizing: border-box;
  //   padding-right: 0.16rem;
  //   /deep/ .van-cell {
  //     padding-right: 0;
  //     width: 2.6rem;
  //   }
  //   /deep/ .van-field__control {
  //     max-width: 3rem;
  //   }
}
/deep/ .van-nav-bar {
  z-index: 99;
}
</style>
